package com.enhinck.dbutilweb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enhinck.dbutilweb.common.WebResponse;
import com.enhinck.dbutilweb.common.database.ConnectionWrapper;
import com.enhinck.dbutilweb.common.database.DbPoolUtil;
import com.enhinck.dbutilweb.common.util.DataTransformer;
import com.enhinck.dbutilweb.model.domain.DatasourceConfDO;
import com.enhinck.dbutilweb.model.vo.DatasourceAddEditConfVO;
import com.enhinck.dbutilweb.model.vo.DatasourceConfSearchVO;
import com.enhinck.dbutilweb.model.vo.DatasourceConfVO;
import com.enhinck.dbutilweb.service.DatasourceConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据源表
 *
 * @author enhinck Generate auto created
 * @date 2021-03-03 10:52:04
 */
@Slf4j
@Api(description = "数据源表管理", tags = {"DatasourceConfController"})
@RestController
@AllArgsConstructor
public class DatasourceConfController {
    private final DatasourceConfService datasourceConfService;
    private final DbPoolUtil dbPoolUtil;

    /**
     * 列表
     *
     * @return
     */
    @ApiOperation("列表")
    @GetMapping("/datasourceconfs")
    public WebResponse<List<DatasourceConfDO>> list() {
        return WebResponse.ok(datasourceConfService.list());
    }

    /**
     * 分页
     *
     * @param searchVO 分页查询条件
     * @return
     */
    @ApiOperation("分页")
    @GetMapping("/datasourceconf/page")
    public WebResponse<Page<DatasourceConfVO>> page(DatasourceConfSearchVO searchVO) {
        Page<DatasourceConfDO> page = new Page(searchVO.getPageNum(), searchVO.getNumPerPage());
        LambdaQueryWrapper<DatasourceConfDO> wrappers = Wrappers.<DatasourceConfDO>query().lambda().orderByDesc(DatasourceConfDO::getGmtUpdate);

        if (StringUtils.isNotBlank(searchVO.getName())) {
            wrappers.like(DatasourceConfDO::getName, searchVO.getName());
        }

        return WebResponse.ok(DataTransformer.transformPage(datasourceConfService.page(page, wrappers), DatasourceConfVO.class));
    }

    /**
     * 新增
     *
     * @param entityVO 新增对象
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/datasourceconf")
    public WebResponse<Boolean> add(@RequestBody DatasourceAddEditConfVO entityVO) {
        return WebResponse.ok(datasourceConfService.save(entityVO.copyPropertiesTo(new DatasourceConfDO())));
    }

    /**
     * 新增
     *
     * @param entityVO 新增对象
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/datasourceconf/form")
    public WebResponse<Boolean> addForm(DatasourceAddEditConfVO entityVO) {
        return WebResponse.ok(datasourceConfService.save(entityVO.copyPropertiesTo(new DatasourceConfDO())));
    }

    /**
     * 更新
     *
     * @param entityVO
     * @return
     */
    @ApiOperation("更新")
    @PostMapping("/datasourceconf/form/update")
    public WebResponse<Boolean> updateForm(DatasourceAddEditConfVO entityVO) {
        return update(entityVO);
    }

    /**
     * 更新
     *
     * @param entityVO
     * @return
     */
    @ApiOperation("更新")
    @PutMapping("/datasourceconf")
    public WebResponse<Boolean> update(@RequestBody DatasourceAddEditConfVO entityVO) {
        DatasourceConfDO bDatasourceConfDO = datasourceConfService.getById(entityVO.getId());
        entityVO.copyPropertiesTo(bDatasourceConfDO);
        bDatasourceConfDO.setGmtUpdate(LocalDateTime.now());
        return WebResponse.ok(datasourceConfService.updateById(bDatasourceConfDO));
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation("查询")
    @GetMapping("/datasourceconf/{id}")
    public WebResponse<DatasourceConfVO> get(@PathVariable("id") Long id) {
        return WebResponse.ok(datasourceConfService.getById(id).copyPropertiesTo(new DatasourceConfVO()));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/datasourceconf/{id}")
    public WebResponse<Boolean> delete(@PathVariable("id") Long id) {
        return WebResponse.ok(datasourceConfService.removeById(id));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @PostMapping("/datasourceconf/form/{id}")
    public WebResponse<Boolean> deleteForm(@PathVariable("id") Long id) {
        return WebResponse.ok(datasourceConfService.removeById(id));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("测试连接")
    @GetMapping("/datasourceconf/test/{id}")
    public WebResponse<Boolean> test(@PathVariable("id") Long id) {
        Boolean flag = false;
        try {
            ConnectionWrapper connectionWrapper = dbPoolUtil.getConnection(id);
            dbPoolUtil.release(connectionWrapper);
            flag = true;
        } catch (Exception e) {
            log.info("{}", e);
        }
        return WebResponse.ok(flag);
    }
}